<template>
  <div class="mod-config">
    <div  v-show="!addOrUpdateVisible">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="任务名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button  type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button  type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        :show-overflow-tooltip="true"
        width="200"
        label="任务名称
">
      </el-table-column>

      <el-table-column
        :show-overflow-tooltip="true"
        prop="tasktime"
        header-align="center"
        align="center"
        width="200px"
        label="任务定时执行时间
">
      </el-table-column>
      <el-table-column
        prop="status"
        :show-overflow-tooltip="true"
        header-align="center"
        align="center"
        label="任务状态">
        <template slot-scope="scope">
          <!--{{ scope.row.status===1?'高级讲师':'首席讲师' }}-->
          <p v-if="scope.row.status===24">新增</p>
          <p v-if="scope.row.status===21">运行中</p>
          <p v-if="scope.row.status===23">执行成功</p>
          <p v-if="scope.row.status===22">执行失败</p>
        </template>

      </el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        width="180px"
        prop="lastruntime"
        header-align="center"
        align="center"
        label="最近一次任务运行时间
">
      </el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        width="150px"
        prop="exechost"
        header-align="center"
        align="center"
        label="执行机器IP">
      </el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        prop="execport"
        header-align="center"
        align="center"
        label="执行机器端口
">
      </el-table-column>
      <el-table-column label="失败日志是否记录" align="center" prop="faillog">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.faillog"
            :active-value=1
            :inactive-value=0
            @change="faillogStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="常规日志是否记录" align="center" prop="commonlog">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.commonlog"
            :active-value=1
            :inactive-value=0
            @change="commonlogStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        prop="maxretry"
        header-align="center"
        align="center"
        label="最大尝试次数
">
      </el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        prop="retryinterval"
        header-align="center"
        align="center"
        label="重试时间间隔
">
      </el-table-column>

      <el-table-column
        prop="description"
        header-align="center"
        align="center"
        :show-overflow-tooltip="true"
        width="150px"
        label="描述
">
      </el-table-column>

      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="stopTask(scope.row.id)"   v-if="scope.row.status===21">关闭</el-button>
          <el-button type="text" size="small" @click="startTask(scope.row.id)"  v-else >启动</el-button>
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id,scope.row.name)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[8, 15, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    </div>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @handleClose="handleClose" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './ssbustask-add-or-update'
  export default {
    data () {
      return {
        flag:true,
        dataForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 8,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      //停止任务
      stopTask(id){
        this.$http({
          url: this.$http.adornUrl("/tbssbustask/stop"),
          method: "get",
          params: this.$http.adornParams({
            id: id,
          }),
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "停止成功",
              type: "success",
              duration: 1500
            });
            this.getDataList();
          } else {
            this.$message.error(data.msg);
          }
        });
      },
      //开启任务
      startTask(id){
        this.$http({
          url: this.$http.adornUrl("/tbssbustask/start"),
          method: "get",
          params: this.$http.adornParams({
            id: id,
          }),
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "开启成功",
              type: "success",
              duration: 1500
            });
            this.getDataList();
          } else {
            this.$message.error(data.msg);
          }
        });

      },

      handleClose(data){
        this.addOrUpdateVisible = data;
        this.getDataList();
      },
      commonlogStatusChange(row){
        //修改常用日志状态
        this.$http({
          url: this.$http.adornUrl("/tbssbustask/upCommonlog"),
          method: "get",
          params: this.$http.adornParams({
            id: row.id,
            commonlog: row.commonlog
          }),
        }).then(({ data }) => {
          if (data && data.code === 0) {
            if(row.commonlog==1) {
              this.$message({
                message: "启用记录",
                type: "success",
                duration: 1500
              });
            }else {
              this.$message({
                message: "取消记录",
                type: "success",
                duration: 1500
              });
            }
          } else {
            this.$message.error(data.msg);
          }
        });

      },
      faillogStatusChange(row){
        //修改失败日志状态
        this.$http({
          url: this.$http.adornUrl("/tbssbustask/upFaillog"),
          method: "get",
          params: this.$http.adornParams({
            id: row.id,
            faillog: row.faillog
          }),
        }).then(({ data }) => {
          if (data && data.code === 0) {
            if(row.faillog==1) {
              this.$message({
                message: "启用记录",
                type: "success",
                duration: 1500
              });
            }else {
              this.$message({
                message: "取消记录",
                type: "success",
                duration: 1500
              });
            }
          } else {
            this.$message.error(data.msg);
          }
        });
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = false
        this.$http({
          url: this.$http.adornUrl('/tbssbustask/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.records
            this.totalPage = data.page.total
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id,name) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        var names = name
          ? [name]
          : this.dataListSelections.map((item) => {
            return item.name;
          });
       /* this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {*/
        this.$confirm(`确定对[名称=${names.join(",")}]进行[${name ? "删除" : "批量删除"}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/tbssbustask/deletes'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList();
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
